import { t } from 'i18next'
import { TAB } from '../store'
import { ISwitch } from '@/api/global/interface'
export interface OrgTreeEntity {
	id: string
	pid: string
	children: OrgTreeEntity[]
	name: string
	user_online_size: number
	user_size: number
	users: {
		mt_key: string
		online: number
	}[]
}
export interface OrgTreeModel {
	org: {
		name: string
		userOnlineSize: number
		userSize: number
		users: {
			mtKey: string
			online: number
		}[]
	}
	id: string
	parentId: string
	status: {
		isOpen: boolean
		isChecked: boolean
	}
	nestingLevel: number
	children: OrgTreeModel[]
}
export interface IOrgTreeUpdateMt {
	mtKey: string
	prevIsOnline: boolean
	nowIsOnline: boolean
}
export const OrgTreeModelTranslator = (org: OrgTreeEntity): OrgTreeModel => {
	//做 第一层 _ 的兼容
	let parentId = org.pid.endsWith('_') ? org.pid.slice(0, -1) : org.pid
	return {
		org: {
			name: org.name,
			userOnlineSize: org.user_online_size,
			userSize: org.user_size,
			users: org.users.map((x) => ({ mtKey: x.mt_key, online: x.online })),
		},
		id: org.id,
		parentId,
		status: {
			isOpen: false,
			isChecked: false,
		},
		nestingLevel: 0,
		children: [],
	}
}
export const OrgFlatTreeTranslator = (mccOrg: OrgTreeEntity[]) => {
	let stack = mccOrg
	let flatArr = []
	while (stack.length > 0) {
		const item = stack.shift()!
		flatArr.push(OrgTreeModelTranslator(item))
		if (item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return flatArr
}
export const OrgCreateTreeTranslator = (flatArr: OrgTreeModel[]): OrgTreeModel[] => {
	let _arr = [...flatArr]
	for (const item of _arr) {
		let children = _arr.filter((x) => x.parentId === item.id)
		item.children = children
	}
	const children = _arr
		.filter((x) => x.parentId === '')
		.map((x) => {
			x.parentId = 'all'
			return x
		})
	const totalUserSize = children.reduce((total, cur) => cur.org.userSize + total, 0)
	const totalUserOnlineSize = children.reduce((total, cur) => cur.org.userOnlineSize + total, 0)
	const resultList = [
		{
			org: {
				name: t('全部终端'),
				userOnlineSize: totalUserOnlineSize,
				userSize: totalUserSize,
				users: [],
			},
			id: 'all',
			parentId: '',
			status: {
				isOpen: false,
				isChecked: false,
			},
			nestingLevel: 0,
			children,
		},
	]
	return resultList
}
export const combineOrgTree = (allTree: OrgTreeModel[], tab: typeof TAB, SWITCH?: ISwitch) => {
	let result = [...allTree]
	if (tab.often.canShow && !!SWITCH?.showOftenMt) {
		result.push({
			org: {
				name: t('常用终端'),
				userOnlineSize: tab.often.onlineSize,
				userSize: tab.often.size,
				users: [],
			},
			id: 'often',
			parentId: '',
			status: {
				isOpen: false,
				isChecked: false,
			},
			nestingLevel: 0,
			children: [],
		})
	}
	if (tab.interactive.canShow) {
		result.push({
			org: {
				name: t('互动终端'),
				userOnlineSize: tab.interactive.onlineSize,
				userSize: tab.interactive.size,
				users: [],
			},
			id: 'interactive',
			parentId: '',
			status: {
				isOpen: false,
				isChecked: false,
			},
			nestingLevel: 0,
			children: [],
		})
	}
	if (tab.live.canShow) {
		result.push({
			org: {
				name: t('直播与会方'),
				userOnlineSize: tab.live.size,
				userSize: tab.live.size,
				users: [],
			},
			id: 'live',
			parentId: '',
			status: {
				isOpen: false,
				isChecked: false,
			},
			nestingLevel: 0,
			children: [],
		})
	}
	if (tab.dcs.canShow) {
		result.push({
			org: {
				name: t('数据协作方'),
				userOnlineSize: tab.dcs.onlineSize,
				userSize: tab.dcs.size,
				users: [],
			},
			id: 'dcs',
			parentId: '',
			status: {
				isOpen: false,
				isChecked: false,
			},
			nestingLevel: 0,
			children: [],
		})
	}

	return result
}
